using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter08
{
	/// <summary>
	/// Example03: The Pendulum 
	/// A pendulum consists of a sphere of mass m attached to 
	/// a light cord of length L, as shown in Figure 8.7. 
	/// The sphere is released from rest at point a 
	/// when the cord makes 
	/// an angle \theta_a with the vertical, and the pivot at P is 
	/// frictionless.
	/// (A) Find the speed of the sphere when it is 
	/// at the lowest point b.
	/// v_b = \sqrt{2gL(1-\cos\theta_a)}
	/// (B) What is the tension in the cord at b?
	/// T_b = mg(3-2\cos\theta_a)
	/// </summary>
	public class Example03
	{
		public Example03()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		
		private L.Mass m = new L.Mass();
		private double g = L.Constant.AccelerationOfGravity;
		public void Compute()
		{
			double theta = 0.1;
			L.Length l = new L.Length();
			l.m = 10.0;
			m.kg = 10.0;
			
			L.Scalar.FunctionOfPosition f = new L.Scalar.FunctionOfPosition(func);
			L.PotentialEnergy U = new L.PotentialEnergy();
            U.ScalarFunctionOfPosition = f;

			L.Position ri = new L.Position();
			ri.X = l.m*Math.Sin(theta);
			ri.Y = -l.m*Math.Cos(theta);
			L.Position rf = new L.Position();
			rf.Y = -l.m;
			L.Work W = new L.Work();
			W.J = -U.Difference(ri,rf);

			L.KineticEnergy ki = new L.KineticEnergy();
			L.KineticEnergy kf = new L.KineticEnergy();
			kf.VariableQ = true;
			L.FundamentalLaw.WorkEnergyTheorem(ki,W,kf);
	
			L.Velocity vf = new L.Velocity(m,kf);
			result += Convert.ToString(vf.mPERs)+"\r\n";
			result += Convert.ToString
				(Math.Sqrt(2.0*g*l.m*(1.0-Math.Cos(theta))))+"\r\n";

			//		T - mg = m v^2 / l
			result += Convert.ToString
				(m.kg*g+m.kg*vf.mPERs*vf.mPERs/l.m)+"\r\n";
			result += Convert.ToString
				(m.kg*g*(3.0-2.0*Math.Cos(theta)))+"\r\n";

	
		}
		private L.Scalar func(L.Position x)
		{
            L.Scalar s = new L.Scalar();
            s.Magnitude = m.kg*g*x.Y;
            return s;
		}
	}
}
